package com.xbfinal.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.reggie.common.CustomException;
import com.xbfinal.reggie.dto.SetmealDto;
import com.xbfinal.reggie.mapper.SetmealMapper;
import com.xbfinal.reggie.pojo.DishFlavor;
import com.xbfinal.reggie.pojo.Setmeal;
import com.xbfinal.reggie.pojo.SetmealDish;
import com.xbfinal.reggie.service.SetmealDishService;
import com.xbfinal.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @autor 笑霸fianl~
 * 欢迎访问GitHub：https://github.com/XBfinal
 * 欢迎访问Gitee：https://gitee.com/XBfianl
 * 欢迎访问CSDN：https://blog.csdn.net/weixin_52062043
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐 同时 保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWitDish(SetmealDto setmealDto) {
        //保存基本信息 执行Setmeal表
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联关系 执行Setmeal_Dish表
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     *  删除套餐还应该删除对应的关联关系
     *
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //只能删除停售的
        LambdaQueryWrapper<Setmeal> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);//售卖状态
        final int count = this.count(queryWrapper);
        if(count>0){
            //如果不能删除，那就抛出异常
            throw new CustomException("套餐在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐中的数据
        this.removeByIds(ids);
        //删除对应关系中的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper1
                =new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);
    }


    /**
     * 修改套餐还应该修改对应的关联关系
     * @param id
     * @return
     */
    @Override
    public SetmealDto updateWithDish(Long id) {

        //1.查套餐基本信息
        final Setmeal setmeal = this.getById(id);
        final SetmealDto setmealDto = new SetmealDto();
        //1.5使用对象拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //2.查套餐关联的信息,从Setmeal_Dish查
        //2.1条件构造器
        LambdaQueryWrapper<SetmealDish> QueryWrapper
                =new LambdaQueryWrapper<>();
        //2.2条件查询
        QueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        final List<SetmealDish> list
                = setmealDishService.list(QueryWrapper);
        setmealDto.setSetmealDishes(list);
        return  setmealDto;
    }

    /**
     * //更新菜品信息，同时更新对应的口味信息
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        //setmealDto
        this.updateById(setmealDto);

        //先清理setmealDish
        LambdaQueryWrapper<SetmealDish> LambdaQueryWrapper
                = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(LambdaQueryWrapper);
        //setmealDish
        List<SetmealDish> flavors = setmealDto.getSetmealDishes();

        flavors=flavors.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(flavors);
    }
}
